package com.ap.core.ldap.dao.api;

import com.ap.core.ldap.domain.LdapAttribute;
import com.ap.core.ldap.domain.LdapZappUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;

import javax.naming.Name;
import javax.naming.directory.*;

/**
 * Abstract class for LDAP generic dao operations
 * @param <T> - Type of LDAP entity
 */
public abstract class AbstractLdapZappUserDao<T> implements LdapGenericDao<T>{

    protected final Logger LOG = LoggerFactory.getLogger(getClass());

    protected LdapTemplate ldapTemplate;

    public void setLdapTemplate(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }



    /**
     * {@inheritDoc}
     */
    @Override
    public void addAttribute(LdapAttribute<?> ldapAttribute) {
        DistinguishedName distinguishedName = new DistinguishedName();
        distinguishedName.add("cn", getDistinguishedGroup());

        Attribute memberInfoAttribute = new BasicAttribute(ldapAttribute.getLdapAttributeName(), ldapAttribute.getLdapAttributeValue());
        ModificationItem memberInfoItem = new ModificationItem(DirContext.ADD_ATTRIBUTE, memberInfoAttribute);
        try{
            ldapTemplate.modifyAttributes(distinguishedName, new ModificationItem[]{memberInfoItem});
        }catch(org.springframework.ldap.AttributeInUseException exception){
            // This catch block if for idempotent transaction if attribute was already added in previous run where db transaction failed.
            LOG.info("The attribute already existed so not adding it again for group Consumerusers = " + ldapAttribute.getLdapAttributeValue());

        }
    }

    @Override
    public void removeAttribute(LdapAttribute<?> ldapAttribute) {
        DistinguishedName distinguishedName = new DistinguishedName();
        distinguishedName.add("cn", getDistinguishedGroup());

        Attribute memberInfoAttribute = new BasicAttribute(ldapAttribute.getLdapAttributeName(), ldapAttribute.getLdapAttributeValue());
        ModificationItem memberInfoItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberInfoAttribute);
        try{
            ldapTemplate.modifyAttributes(distinguishedName, new ModificationItem[]{memberInfoItem});
        } catch(org.springframework.ldap.AttributeInUseException exception){
            // This catch block if for idempotent transaction if attribute was not added in previous run where db transaction failed.
            LOG.info("The attribute not existed so not able to delete it for group Consumerusers = " + ldapAttribute.getLdapAttributeValue());
        }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(Name distinguishedName) {
        ldapTemplate.unbind(distinguishedName);
    }

    /**
     * Get the Distinguished Group
     *
     */
    protected abstract String getDistinguishedGroup();
}
